//
//  MallClassViewController.m
//  ShouYi
//
//  Created by yrapp on 16/4/18.
//  Copyright © 2016年 yurui. All rights reserved.
//

#import "MallClassViewController.h"
#import "MallCollectionViewCell.h"
@interface MallClassViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (nonatomic,strong)UIScrollView *titleScrollView;
@property (nonatomic,strong)UICollectionView *collectionView;

@property(nonatomic,strong)NSMutableArray *proListArray;

@end

@implementation MallClassViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    //NavigationBar
    [self addNavigationBar];
    
    //添加标题
    [self addTitle];
    //添加商品列表
    [self addCollectionView];
    //解析主界面商品展示
    [self updataForMainlist];
    // Do any additional setup after loading the view.
}
#pragma mark --- 添加视图 ---
- (void)addNavigationBar {
    
    self.navigationItem.title = @"商城";
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     
                                                                     [UIColor whiteColor], UITextAttributeTextColor,
                                                                   
                                                                     nil]];
    
    UIButton *backButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [backButton setImage:[UIImage imageNamed:@"icon_fh"] forState:(UIControlStateNormal)];
    backButton.frame = CGRectMake(0, 0, 32, 32);
    [backButton addTarget:self action:@selector(back) forControlEvents:(UIControlEventTouchUpInside)];
    backButton.imageEdgeInsets = UIEdgeInsetsMake(6, 0, 6, 20);
    
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftButton;

}

//添加标题
- (void)addTitle {
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.titleScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, kWidth, 30)];
    NSArray *array = @[@"全部",@"服饰",@"电器",@"食品",@"娱乐",@"科技",@"笔记本",@"手机"];
    for (int i  = 0; i < array.count; i ++) {
        
        UIButton *button = [UIButton buttonWithType:(UIButtonTypeSystem)];
        button.frame = CGRectMake(50 * i, 0, 50 , 30);
        button.tag = 1000 + i;
        [button setTitle:array[i] forState:(UIControlStateNormal)];
        [button setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        [button addTarget:self action:@selector(titleButton:) forControlEvents:(UIControlEventTouchUpInside)];
        [self.titleScrollView addSubview:button];
        
    }
    self.titleScrollView.contentSize = CGSizeMake(array.count * 50, 30);
    self.titleScrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:self.titleScrollView];
    
}
//添加商品列表
- (void)addCollectionView {
    // 创建一个布局对象
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    // 设置集合视图行间距
    layout.minimumLineSpacing = 13;
    //设置集合视图 每个item之间的间隔
    layout.minimumInteritemSpacing = 12;
    
    //设置集合视图中每个item的大小
    layout.itemSize = CGSizeMake((kWidth - 36) / 2, kWidth/ 2 + 20);
    
    // 创建一个集合视图
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0,64+30, kWidth, kHeight + 64) collectionViewLayout:layout];
    
    //self.collectionView.
    self.collectionView.contentInset = UIEdgeInsetsMake(0, 12, 0, 12);
    
    self.collectionView.backgroundColor = [UIColor colorWithRed:236.0/255 green:240.0/255 blue:242.0/255 alpha:1];
    
    // 指定集合视图的数据源
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    
    //注册cell
    [self.collectionView registerClass:[MallCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];

    [self.view addSubview:self.collectionView];

    
}

#pragma mark --- 按钮功能实现 ---
//title按钮功能实现
- (void)titleButton:(UIButton *)button {
    //测试数据
    NSArray *array = @[@"全部",@"服饰",@"电器",@"食品",@"娱乐",@"科技",@"笔记本",@"手机"];

    for (int i = 0; i < array.count; i++) {
        
        UIButton *allButton = [self.view viewWithTag:1000 + i];
        [allButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    }
    
    [button setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];
    
}
//返回上一个界面
- (void)back {
    
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

#pragma mark --- 数据处理 ---

- (NSMutableArray *)proListArray {
    if (!_proListArray) {
        _proListArray = [NSMutableArray array];
    }
    return _proListArray;
}


//商品列表
- (void)updataForMainlist {
    
    
    NSString *cuuid = [[NSUserDefaults standardUserDefaults] objectForKey:@"CFUUID"];
    NSString *url = [KMainList stringByAppendingString:[NSString stringWithFormat:@"&uuid=%@",cuuid]];
    
    [LORequestManger GET:url success:^(id response) {
        NSArray *array = (NSArray *)response;
        
        for (NSDictionary *dic in array) {
            prolistModel *model = [prolistModel setUpdateDic:dic];
            [self.proListArray addObject:model];
        }
        
        [self.collectionView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
    
}

#pragma mark --- collectionView ---
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.proListArray.count;
}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    
    return 1;
    
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    MallCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    prolistModel *model = self.proListArray[indexPath.row];
    
    [cell setModel:model];
    
    return cell;
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
